<?php

class userticket extends spModel
{
	public $pk = "id"; // 数据表的主键
    public $table = "userticket"; // 数据表的名称

	function create($row) {
		$mTicket = spClass('ticket');
		$ret   = $mTicket->find(array('id'=>$row['tid']));
		$this->runSql("UPDATE tickets SET sold = sold+1 WHERE id = ".$row['tid']);

		$mUser = spClass('user');
		$mUser->runSql("UPDATE user SET cash = cash-".$ret['value']." WHERE uid = ".$row['uid']);

		$mUserLog = spClass('userlog');
		$logArray	  = array(
							'uid' => $row['uid'],
							'type'=> 'buyticket',
							'content' => 'Use '. $ret['value']. ' for a ticket ' . $ret['name'],
							'dateline'=> time()
						);
		$lastLogId    = $mUserLog->create($logArray);

		$cashLogArray	  = array(
							'uid' => $row['uid'],
							'logid' => $lastLogId,
							'amount'=> -$ret['value'],
							'dateline'=> time()
						);
		$mUserCashLog = spClass('usercashlog');
		$mUserCashLog->create($cashLogArray);

		parent::create($row);
	}

}